// 功能块
import {FuncBlock} from "../../../component/FuncBlock/FuncBlock.js";

let bluetoothClockFuncBlock = FuncBlock(document.querySelector(".bluetoothClockFuncBlock"));
bluetoothClockFuncBlock.setText("Timer PowerOff");
bluetoothClockFuncBlock.setIcon("./img/DeviceControl/FuncBlock/定时.svg");

// 拓展块控制
function addFuncBlockTail() {
    let bluetoothClockBox = document.createElement("div");
    bluetoothClockBox.className = "bluetoothClockBox";
    bluetoothClockBox.innerHTML = `
        <!-- 滚动时钟容器 -->
        <div class="bluetoothClock"></div>
        <div class="block"></div>
        <!-- 滚动按钮容器 -->
        <div class="bluetoothButton"> Click Start </div>
    `;

    // 加入尾部
    bluetoothClockFuncBlock.setTailBox(bluetoothClockBox);
}
addFuncBlockTail();

import {ScrollClock} from "../../../component/ScrollClock/ScrollClock.js";

let bluetoothClock = ScrollClock(bluetoothClockFuncBlock.getCup().querySelector(".bluetoothClock"));

// 添加功能
bluetoothClock.addForceScroll();
bluetoothClock.addTimeMode();

import {ScrollButton} from "../../../component/ScrollButton/ScrollButton.js";

let bluetoothButton = ScrollButton(bluetoothClockFuncBlock.getCup().querySelector(".bluetoothButton"));

// 时钟按钮控制
bluetoothClock.addButton(bluetoothButton.getScrollButton());

// 大小自适应
let timeout = null;
bluetoothClock.setSize(1);
bluetoothButton.setSize(1);
bluetoothClockFuncBlock.setSize(1);
window.addEventListener("resize", () => {
    clearTimeout(timeout);
    timeout = setTimeout(() => {
        bluetoothClock.setSize(1);
        bluetoothButton.setSize(1);
        bluetoothClockFuncBlock.setSize(1);
    }, 200);
});

// 设置功能块点击时回调函数
bluetoothClockFuncBlock.setCheckedCallBack(() => {
    bluetoothButton.getScrollButton().style.pointerEvents = "";
    bluetoothButton.getScrollButton().click();
    bluetoothButton.getScrollButton().click();
});

let funcBlockTailblock = bluetoothClockFuncBlock.getCup().querySelector(".block");
let clockSignBoxList = bluetoothClock.getCup().querySelectorAll(".signBox");

// 设置取消功能块点击时回调函数
bluetoothClockFuncBlock.setNoCheckedCallBack(() => {
    bluetoothButton.getScrollButton().style.pointerEvents = "";
    bluetoothButton.getScrollButton().click();
    bluetoothButton.getScrollButton().click();

    // 恢复正常大小
    bluetoothButton.getCup().style.maxWidth = "50%";
    funcBlockTailblock.style.width = "13.5%";
    clockSignBoxList.forEach((clockSignBox) => {
        clockSignBox.style.width = "var(--SCBSignWide)";
    });
});

// 启动监听
bluetoothClockFuncBlock.addClickRespon();

// 获取蓝牙
import {theService} from "./BluetoothLink.js";

// 启动后变形
function addTranform() {
    let clockClickPoint = bluetoothClock.getCup().querySelector(".clickPoint");

    bluetoothButton.addClickRespon();
    // 设置回调函数
    bluetoothButton.setClickCallBack(() => {
        setTimeout(() => {
            if(typeof clockClickPoint.innerText !== "string" || clockClickPoint.innerText === "0")
                return;

            bluetoothButton.getScrollButton().style.pointerEvents = "none";
            bluetoothButton.getCup().style.maxWidth = "0";

            funcBlockTailblock.style.width = "0";

            clockSignBoxList.forEach((clockSignBox) => {
                clockSignBox.style.width = "calc(var(--SCBSignWide) * 3)";
        }, 200);

        });
    });

    // 计时结束时间 使用闭包
    function endEvent() {
        theService.sendHexData(0xffe0, 0xffe2, "0");
    }
    bluetoothClock.addEndFunction(endEvent);
}
addTranform();
